package com.prs.crm.action.ca.work;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.action.warning.AutoCodeAction;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.ca.work.ArticleInfo;
import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.domain.ca.work.DemandInventory;
import com.prs.crm.domain.ca.work.ProcureStore;
import com.prs.crm.domain.warning.AutoCode;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.approval.impl.ShenQingTypeServiceImpl;
import com.prs.crm.service.ca.work.ArticleInfoService;
import com.prs.crm.service.ca.work.ArticleTypesService;
import com.prs.crm.service.ca.work.DemandInventoryService;
import com.prs.crm.service.ca.work.ProcureStoreService;
import com.prs.crm.service.warning.AutoCodeService;
/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 10:34:29 采购入库
 */
public class ProcureStoreAction extends BaseAction{

	private static final long serialVersionUID = -134522440631896560L;
	private ProcureStoreService procureStoreService;
	private PaginationSupport pagination;
	private ProcureStore procureStore;
	private Integer[] ids;
	private Application application;
	private ApplicationService ApplicationService;
	
	private List<ArticleInfo> articleInfo;
	private ArticleInfoService articleInfoService;
	
	private List<ArticleTypes> articleTypes;
	private ArticleTypesService articleTypesService;
	private ShenQingTypeService shenQingTypeService = new ShenQingTypeServiceImpl();
	
	private DemandInventoryService demandInventoryService;
	private AutoCodeService autoCodeService;
	private String codeNum;
	public AutoCodeService getAutoCodeService() {
		return autoCodeService;
	}

	public void setAutoCodeService(AutoCodeService autoCodeService) {
		this.autoCodeService = autoCodeService;
	}

	public String getCodeNum() {
		return codeNum;
	}

	public void setCodeNum(String codeNum) {
		this.codeNum = codeNum;
	}

	public DemandInventoryService getDemandInventoryService() {
		return demandInventoryService;
	}

	public void setDemandInventoryService(
			DemandInventoryService demandInventoryService) {
		this.demandInventoryService = demandInventoryService;
	}

	public ShenQingTypeService getShenQingTypeService() {
		return shenQingTypeService;
	}

	public void setShenQingTypeService(ShenQingTypeService shenQingTypeService) {
		this.shenQingTypeService = shenQingTypeService;
	}

	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public ApplicationService getApplicationService() {
		return ApplicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}

	public List<ArticleInfo> getArticleInfo() {
		return articleInfo;
	}

	public void setArticleInfo(List<ArticleInfo> articleInfo) {
		this.articleInfo = articleInfo;
	}

	public ArticleInfoService getArticleInfoService() {
		return articleInfoService;
	}

	public void setArticleInfoService(ArticleInfoService articleInfoService) {
		this.articleInfoService = articleInfoService;
	}

	public List<ArticleTypes> getArticleTypes() {
		return articleTypes;
	}

	public void setArticleTypes(List<ArticleTypes> articleTypes) {
		this.articleTypes = articleTypes;
	}
	public ArticleTypesService getArticleTypesService() {
		return articleTypesService;
	}

	public void setArticleTypesService(ArticleTypesService articleTypesService) {
		this.articleTypesService = articleTypesService;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public ProcureStore getProcureStore() {
		return procureStore;
	}

	public void setProcureStore(ProcureStore procureStore) {
		this.procureStore = procureStore;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public ProcureStoreService getProcureStoreService() {
		return procureStoreService;
	}

	public void setProcureStoreService(ProcureStoreService procureStoreService) {
		this.procureStoreService = procureStoreService;
	}
	
	@Action("listProcureStore")
	public String listProcureStore(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ProcureStore.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProcureStoreService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("listStoreParticular")
	public String listStorePaticular(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ProcureStore.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProcureStoreService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("viewStoreParticular")
	public String viewStoreParticular(){
		this.articleInfo=this.getArticleInfoService().getAllAticleInfo();
		this.articleTypes=this.getArticleTypesService().getAllArticleTypes();
			return SUCCESS;
	}
	@Action(value = "saveProcureStore", results = {
			@Result(name = "success", type = "redirect", location = "listProcureStore"),
			@Result(name = "error", type = "chain", location = "editProcureStore") })
	public String saveProcureStore(){
		if(procureStore.getPs_storeDate()==null){
			procureStore.setPs_storeDate(new Date());
		}
		if("".equals(procureStore.getPs_billNumber())){
			this.addActionError("单号不能为空！");
			return ERROR;
		}
		if(procureStore.getPs_number()==null){
			this.addActionError("数量不能为空！");
			return ERROR;
		}
		this.getProcureStoreService().save(procureStore);
		System.out.println(procureStore.getArticleInfo().getAi_articleName());
		
		DemandInventory di=this.getDemandInventoryService().getDemandInventoryByNames(procureStore.getArticleInfo().getAi_articleName());
		if(di==null){
			di=new DemandInventory();
			di.setDi_articletype(procureStore.getArticleInfo().getAi_articleType());
			di.setDi_articleName(procureStore.getArticleInfo().getAi_articleName());
			di.setDi_articleStandard(procureStore.getArticleInfo().getAi_articleStandard());
			di.setDi_articleNumber(String.valueOf(procureStore.getPs_number()));
			di.setDi_articleUnit(procureStore.getArticleInfo().getAi_articleUnit());
			di.setDi_articleUnivalence(procureStore.getArticleInfo().getAi_articleUnivalence());
			di.setDi_money(procureStore.getPs_money());
			this.getDemandInventoryService().save(di);
		}else{
			Integer num1=Integer.parseInt(di.getDi_articleNumber());
			Integer allNum=num1+procureStore.getPs_number();
			di.setDi_articleNumber(String.valueOf(allNum));
			System.out.println(allNum);
			double danjia=Double.parseDouble(di.getDi_articleUnivalence());
			double num=(double)allNum;
			System.out.println(danjia*num);
			di.setDi_money(String.valueOf(danjia*num));
			this.getDemandInventoryService().submit(di);
		}
		
		
//		application = new Application();
//		application.setType("一般");
//		application.setAppName("[一般]"+"采购入库");
//		application.setSqType(2);
//		application.setSpr(0);
//		application.setPerson(procureStore.getRecorder());
//		application.setResign("采购入库");
//		application.setProcureStore(procureStore);
//		//application.setShenQingType(this.getShenQingTypeService().getShenQingType("采购入库"));
//		this.getApplicationService().save(application);
		return SUCCESS;
	}
	
	@Action(value = "removeProcureStore", results = {
			@Result(name = "success", type = "redirect", location = "listProcureStore"),
			@Result(name = "error", type = "chain", location = "listProcureStore") })
	public String removeProcureStore(){
		if (ids==null) {
			this.addActionError("请选中一行进行操作！");
			return ERROR;
		}
		this.getProcureStoreService().batchRemove(ids);
		this.setPrompt("删除成功！");
		return SUCCESS;
	}
	
	@Action("editProcureStore")
	public String edit(){
		codeNum=getCode("RK");
		this.articleInfo=this.getArticleInfoService().getAllAticleInfo();
		this.articleTypes=this.getArticleTypesService().getAllArticleTypes();
		return SUCCESS;
	}
	/***
	 * 根据类型进行编码的保存
	 * 
	 * @param type
	 */
	public String getCode(String type) {
	    AutoCode alist=this.getAutoCodeService().getTaskRemind(type);
	    Date dat=new Date();
	    SimpleDateFormat adf=new SimpleDateFormat("yyyyMMdd");
	    String date=adf.format(dat);
	    if(alist==null)
	    {//初始的时候调用
		    AutoCode  ac=new AutoCode();
		    ac.setCodeType(type);
		    ac.setCodeValue(type+date+"0000001");
		    ac.setAddValue("0000001");
		    this.getAutoCodeService().save(ac);	
		    return type+date+"0000001";
	    }else{
	    	String vac=alist.getAddValue();
	    	AutoCode alistA=this.getAutoCodeService().getTaskRemind(type);
	    	alistA.setCodeValue(type+date+new AutoCodeAction().addCodes(vac));
	    	alistA.setAddValue(new AutoCodeAction().addCodes(vac));
		    this.getAutoCodeService().submit(alistA);	
		    return type+date+new AutoCodeAction().addCodes(vac);
	    }
	}
}
